package com.financing.wap.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 活动规则工具类
 * @author Marco
 *
 */
public class ActiviesRuleUtils {
	static Logger logger = Logger.getLogger(ActiviesRuleUtils.class);
	static Map<String,Integer> cacheMap = new HashMap<String,Integer>();
	/**
	 * 根据答案获取档位
	 * @param sessionId
	 * @param answer
	 * @return
	 */
	public static Integer questionLevel(Map<String,String> answerMap){
		String otherTag = "0_";
		String a1 = answerMap.get("1");
		String a2 = answerMap.get("2");
		String a3 = answerMap.get("3");
		String a13 = answerMap.get("13");
		String a17 = answerMap.get("17");
		String a14 = answerMap.get("14");
		String a16 = answerMap.get("16");
		logger.info(">>start level1");
		Integer level = 1;			
		if(a1.indexOf(otherTag)==0){ //题1选择最后一项()
			return level;
		}
		if(a14.equals("6")){ //题14选择最后2项()
			return level;
		}
		if(a16.equals("5")||a16.equals("6")){ //题1选择最后2项()
			return level;
		}
		if(a3.equals("2")){ //题3选择否
			return level;
		}
		if(a2.equals("1")&&a13.equals("4")){ //题3选择否
			return level;
		}
		logger.info(">>start level3");
//		题3 是    题13	最后1个选项（+20）	
//		题3 是    题17	最后2选项之一（+15）
		level =3;
		if(a3.equals("1")&&a13.equals("4")&&(a17.equals("4")||a17.equals("5"))){ //题3选择否
			return level;
		}
		logger.info(">>others is level2");
		level=2;
		return level;

	}
	
	/**
	 * 根据档位和档位中设定的概率算出分
	 * @param level
	 * @return
	 */
	public static Integer questionScore(Integer level,boolean isFull){
		Integer score = 188;
		
		if(level==1){
			logger.info(">>score:l1,188hb");
			return score;
		}
		
		if(level==2){
//			int num = RandomUtils.getRandomNumUtils(0,100);
//			if(num<20){//20的概率20~29元
//				
//				score = RandomUtils.getRandomNumUtils(20, 30);
//				logger.info(">>score:l2,20% score:"+score);
//			}else{//80的概率10-19
//				score = RandomUtils.getRandomNumUtils(10, 20);
//				logger.info(">>score:l2,80% score:"+score);
//			}
			score = 2;
			logger.info(">>score:l2,2yuan");
			return score;
		}
		 
		if(level==3){
			score = 5;
			int num = RandomUtils.getRandomNumUtils(0,100);
			if(num<95){//95的概率30元
				score = 5;
				logger.info(">>score:l3,95% score:"+score);
			}
			if(95<=num&&num<99){//4的概率30-100
				score = RandomUtils.getRandomNumUtils(5, 21);
				logger.info(">>score:l3,99% score:"+score);
			}
			if(99==num&&!isFull){//1的概率500
				score = 500;
				logger.info(">>score:l3,1% 500 score:"+score);
			}
			return score;
		}
		return score;
	}
	
	/**
	 * 将问题英文变为数字
	 * @param t
	 * @return
	 */
	public static String getQuestionId(String t){

		if("one".equals(t)){
			return "1";
		}
		if("two".equals(t)){
			return "2";
		}
		if("three".equals(t)){
			return "3";
		}
		if("four".equals(t)){
			return "4";
		}
		if("five".equals(t)){
			return "5";
		}
		if("six".equals(t)){
			return "6";
		}
		if("seven".equals(t)){
			return "7";
		}
		if("eight".equals(t)){
			return "8";
		}
		if("nine".equals(t)){
			return "9";
		}
		if("ten".equals(t)){
			return "10";
		}
		if("eleven".equals(t)){
			return "11";
		}
		if("twelve".equals(t)){
			return "12";
		}
		if("thirteen".equals(t)){
			return "13";
		}
		if("fourteen".equals(t)){
			return "14";
		}
		if("fifteen".equals(t)){
			return "15";
		}
		if("sixteen".equals(t)){
			return "16";
		}
		if("seventeen".equals(t)){
			return "17";
		}
		if("eighteen".equals(t)){
			return "18";
		}
		if("nineteen".equals(t)){
			return "19";
		}
		if("twenty".equals(t)){
			return "20";
		}
		return "0";
	}
	
	/**
	 * 将答案json转成map
	 * @param jsonStr
	 * @return
	 */
	public static Map<String,String> getAnswerMap(String jsonStr){
		Map<String,String> answerMap = new HashMap<String,String>();
		JSONObject json = JSON.parseObject(jsonStr);
		Set<String> keySet = json.keySet();
		Iterator<String> keyIterator = keySet.iterator();
		while(keyIterator.hasNext()){
			String key = keyIterator.next();
			answerMap.put(getQuestionId(key), (json.get(key).toString()));
		}
		return answerMap;
	}
	
	 
	
	public static void main(String[] orgs){
		String qajson = "{'one':'2','two':'2','three':'1','four':'2','five':'','six':'3','seven':'','eight':'3','nine':'','ten':'','eleven':'','twelve':'3','thirteen':'3','fourteen':'1','fifteen':'4','sixteen':'2','seventeen':'4','eighteen':'1','nineteen':'2'}";
		 Map<String,String> answerMap  = ActiviesRuleUtils.getAnswerMap(qajson);
		Integer l = ActiviesRuleUtils.questionLevel(answerMap);
		Integer score = ActiviesRuleUtils.questionScore(l,false);
		System.out.println(score);
	}
}
